import {Component, OnInit} from '@angular/core';
import {Project} from "../../project/entity/Project";
import {ProjectFindService} from "../../project/service/project.find.service";
import {Params, ActivatedRoute, Router, NavigationExtras} from "@angular/router";
import 'rxjs/add/operator/switchMap';
import {Page} from "../../shared/entity/Page";
import {ProjectCategory} from "../entity/ProjectCategory";
import {CategoryFindService} from "../service/category.find.service";
@Component({
  selector: 'category-children',
  templateUrl: './category-children-list.html'
})
export class CategoryChildrenComponent implements OnInit{
  // public page:Page<Project> = new Page<Project>();
  public categoryListTwo:Array<ProjectCategory> = new Array<ProjectCategory>();
  public categoryListThree:Array<ProjectCategory> = new Array<ProjectCategory>();

  result:string;
  paramsId:string;
  private thirdCategoryRequest:boolean = false;
  ngOnInit(): void{
    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) =>
        this.categoryFindService.findByParendId(this.paramsId = params["id"]))
      .subscribe(categoryListTwo =>
      {
        if(this.paramsId==null||this.paramsId==""){
          this.categoryListTwo = JSON.parse(sessionStorage.getItem("categoryListTwo"));
          this.categoryListThree = JSON.parse(sessionStorage.getItem("categoryListThree"));
          return;
        }
        if(this.thirdCategoryRequest){
          this.thirdCategoryRequest = false;
        }else{
          this.categoryListTwo = categoryListTwo;
          sessionStorage.setItem("categoryListTwo",JSON.stringify(categoryListTwo));
          this.categoryListThree  = new Array<ProjectCategory>();
        }

      });



    //
    // this.activatedRouter.params
    // // (+) converts string 'id' to a number
    //   .switchMap((params: Params) => this.projectService.getProjectPageByCategory(params["categoryId"]))
    //   .subscribe((page: Page<Project>) => this.page = page);
  }


  constructor(
    private activatedRouter: ActivatedRoute,
    private projectService: ProjectFindService,
    private router:Router,
    private categoryFindService:CategoryFindService) {
  }


  private init(id: string, pageNo: string,indexOne:string):string {
    let categoryList = new Array<ProjectCategory>();
    categoryList = JSON.parse(sessionStorage.getItem("categoryList"));
    let   currentOneCategoryId = sessionStorage.getItem("currentOneCategoryId");
    // let   indexOne = sessionStorage.getItem("indexOne");
    this.categoryListTwo = categoryList[indexOne].childList;
    return "success";
  }
  public toCategoryNavTwo(categoryId:string,indexTwo:string){
    // let categoryListTwo = JSON.parse(sessionStorage.getItem("categoryList"));
    let   currentOneCategoryId = sessionStorage.getItem("currentOneCategoryId");
    // let   indexOne = sessionStorage.getItem("indexOne");
    this.categoryFindService.findByParendId(categoryId)
      .then(categoryListThree => {
        this.categoryListThree = categoryListThree;
        sessionStorage.setItem("categoryListThree",JSON.stringify(categoryListThree));
      });
    ;
  }


  public toPage(categoryId:string){
    let width = document.body.clientWidth;
    console.log("toPage");
    console.log(width+"<450="+(width<450));
    if(width<450){
      console.log("none");
      $("#category-child-list").css("display","none");
      $("#category-child-list").css("display","none");
    }

    this.thirdCategoryRequest=true;
    this.router.navigate(['/category1/pageData',{id:categoryId+"", pageNo: 0}]);
    // let navigationExtras: NavigationExtras = {
    //   queryParams: { id:categoryId+"", pageNo: 0},
    //   fragment: 'anchor'
    // };
    //
    // this.router.navigate([{ outlets: { pageData: ['page-data']}}],navigationExtras);
  }
}
